﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class cancel_ticket_ans2 : System.Web.UI.Page
    {
        float charges, tax, sum, change, show_sum=0, show_change2=0;
        string seat;
        string show_ticket, show_seat, show_count;
        int chk = 0,ssid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {
                        string[] data_cancel = (string[])Session["data_cancel"];
                        string[] data_ticket = (string[])Session["data_ticket"];
                        try
                        {
                            string[] data_ticket_print = (string[])Session["data_ticket_print"];
                            chk++;
                        }
                        catch (Exception)
                        {
                            //string[] data_ticket_print = 0.ToString();
                        }                        
                        for (int i = 0; i < data_cancel.Length; i++)
                        {
                            string point = @"select *
                            from Table_Sale
                            inner join Table_SaleDetail
	                            on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
	                        inner join Table_Promotion
								on Table_Sale.Promotion_id = Table_Promotion.Promotion_id
							inner join Table_Passenger
								on Table_Sale.Passenger_id = Table_Passenger.Passenger_id
							inner join Table_Category
								on Table_Passenger.Category_id = Table_Category.Category_id
                            where Table_Sale.Sale_id = '" + data_ticket[i] + "'";
                            SqlCommand sqlcomm = new SqlCommand();
                            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                            SqlConnection sqlconn = new SqlConnection(Connection);
                            SqlDataAdapter da = new SqlDataAdapter(point, sqlconn);
                            DataTable dt = new DataTable();
                            da.Fill(dt);
                            if (dt.Rows.Count <=0)
                            {
                                
                            }
                            else
                            {
                                try
                                {
                                    charges = float.Parse(dt.Rows[0]["Price_ticket"].ToString());
                                    tax = float.Parse(dt.Rows[0]["tex"].ToString());
                                    seat = dt.Rows[0]["Seat_no"].ToString();
                                    float count = float.Parse(data_cancel[i]);
                                    float discount_user = float.Parse(dt.Rows[0]["Category_Discount"].ToString());
                                    float promotion = float.Parse(dt.Rows[0]["Promotion_discount"].ToString());
                                    //sum = (float)(charges + tax);
                                    change = charges - ((charges * (promotion + discount_user)) / 100);
                                    float change2 = change + tax;
                                    sum = (change + tax) - (((change + tax) * count) / 100);
                                    //TextBox1.Text = seat.ToString();
                                    //TextBox2.Text = change2.ToString();
                                    //TextBox4.Text = sum.ToString();
                                    //TextBox5.Text = count.ToString();
                                    //Session["count"] = count;
                                    if (i==0)
                                    {
                                        show_ticket = data_ticket[i];
                                        show_seat = seat;
                                        show_count = data_cancel[i];
                                    }
                                    else
                                    {
                                        show_ticket = show_ticket+"," + data_ticket[i];
                                        show_seat = show_seat+"," + seat;
                                        show_count = show_count+"," + data_cancel[i];
                                    }
                                    show_change2 = show_change2 + change2;
                                    show_sum = show_sum + sum;
                                    ssid = Int32.Parse(dt.Rows[0]["Personnel_id"].ToString());
                                }
                                catch (Exception)
                                {
                                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วได้');", true);
                                    //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                                }
                            }
                        }
                        TextBox1.Text = show_ticket.ToString();
                        TextBox6.Text = show_seat.ToString();
                        TextBox2.Text = show_change2.ToString();
                        TextBox5.Text = show_count.ToString();
                        TextBox4.Text = show_sum.ToString();
                        Session["chk"] = chk;
                        Session["ssid"] = ssid;
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
            }    
        }

        protected void Button1_Click(object sender, EventArgs e)
        {   string[] data_cancel = (string[])Session["data_cancel"];
            string[] data_ticket = (string[])Session["data_ticket"];
            try
            {
                string[] data_ticket_print = (string[])Session["data_ticket_print"];
            }
            catch (Exception)
            {
                //string[] data_ticket_print = 0.ToString();
            }      
            for (int i = 0; i < data_cancel.Length; i++)
            {
                float count = float.Parse(data_cancel[i]);
                count = 100 - count;
                DataClasses1DataContext db = new DataClasses1DataContext();
                try
                {
                    var q = from p in db.Table_Sales
                            where p.Sale_id == data_ticket[i]
                            select p;
                    q.ToList().ForEach(c => c.SSID = 4);
                    q.ToList().ForEach(c => c.percentage = count);
                    //db.SubmitChanges();
                    //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('คืนตั๋วเรียบร้อย');", true);
                    ////Response.Write(@"<script language='javascript'>alert('คืนตั๋วเรียบร้อย');</script>");
                    //Response.Redirect("cancel_ticket.aspx");
                }
                catch (Exception)
                {
                    //ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วได้');", true);
                    ////Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                    //Response.Redirect("cancel_ticket.aspx");
                }      

            }
            try
            {
                if (Int32.Parse(Session["chk"].ToString()) > 0)
                {
                    if (Session["ssid"].ToString() ==null)
                    {
                        Session["ticket_print"] = (string[])Session["data_ticket_print"];
                        Response.Redirect("report_ticket2.aspx"); 
                    }
                    else
                    {
                        Session["ticket_print"] = (string[])Session["data_ticket_print"];
                        Response.Redirect("report_ticket.aspx"); 
                    }
                           
                }
            }
            catch (Exception)
            {
                if (Session["ssid"].ToString() == null)
                {
                    Session["ticket_print"] = (string[])Session["data_ticket_print"];
                    Response.Redirect("report_ticket2.aspx");
                }
                else
                {
                    Session["ticket_print"] = (string[])Session["data_ticket_print"];
                    Response.Redirect("report_ticket.aspx");
                }
            }
            Response.Redirect("cancel_ticket.aspx");        
                 
        }
    }
}